
package com.xifan.myaccount;

import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;

import com.xifan.myaccount.entity.RecordDetail;
import com.xifan.myaccount.fragments.StatFragment;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

import java.util.ArrayList;
import java.util.List;

public class Statistics extends SwipeBackActivity {

    private List<Fragment> mFragmentList;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_record);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        final ImageView mIndicator1st = (ImageView) findViewById(R.id.indicator_1);
        final ImageView mIndicator2nd = (ImageView) findViewById(R.id.indicator_2);
        final Drawable currentIndicator = getResources().getDrawable(
                R.drawable.ic_indicator_current);
        final Drawable defaultIndicator = getResources().getDrawable(
                R.drawable.ic_indicator_default);

        FragmentManager fm = getFragmentManager();
        mFragmentList = new ArrayList<Fragment>();

        StatFragment s1 = new StatFragment();
        Bundle b1 = new Bundle();
        b1.putInt("opType", RecordDetail.TYPE_EXPEND);
        s1.setArguments(b1);
        StatFragment s2 = new StatFragment();
        Bundle b2 = new Bundle();
        b2.putInt("opType", RecordDetail.TYPE_REVENUE);
        s2.setArguments(b2);
        mFragmentList.add(s1);
        mFragmentList.add(s2);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(new OperateTypeAdapter(fm));
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            public void onPageSelected(int index) {
                switch (index) {
                    case 0:
                        mIndicator1st.setImageDrawable(currentIndicator);
                        mIndicator2nd.setImageDrawable(defaultIndicator);
                        break;
                    case 1:
                        mIndicator1st.setImageDrawable(defaultIndicator);
                        mIndicator2nd.setImageDrawable(currentIndicator);
                        break;
                }
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.stats_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private class OperateTypeAdapter extends FragmentPagerAdapter {
        public OperateTypeAdapter(FragmentManager fm) {
            super(fm);
        }

        public Fragment getItem(int location) {
            return mFragmentList.get(location);
        }

        public int getCount() {
            return mFragmentList.size();
        }

    }
}
